package com.ruoyi.web.controller.system

import com.ruoyi.common.annotation.Log
import com.ruoyi.common.core.controller.BaseController
import com.ruoyi.common.core.domain.AjaxResult
import com.ruoyi.common.core.page.TableDataInfo
import com.ruoyi.common.enums.BusinessType
import com.ruoyi.common.utils.poi.ExcelUtil
import com.ruoyi.system.domain.SysPost
import com.ruoyi.system.service.ISysPostService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletResponse

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/post")
open class SysPostController : BaseController() {

    @Autowired
    private lateinit var postService: ISysPostService

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    open  fun list(post: SysPost): TableDataInfo {
        startPage()
        val list = postService.selectPostList(post)
        return getDataTable(list)
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @PostMapping("/export")
    open  fun export(response: HttpServletResponse, post: SysPost) {
        val list = postService.selectPostList(post)
        val util: ExcelUtil<SysPost> = ExcelUtil(SysPost::class.java)
        util.exportExcel(response, list, "岗位数据")
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = ["/{postId}"])
    open  fun getInfo(@PathVariable postId: Long?): AjaxResult {
        return success(postService.selectPostById(postId))
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    open fun add(@Validated @RequestBody post: SysPost): AjaxResult {
        if (!postService.checkPostNameUnique(post)) {
            return error("新增岗位'" + post.postName + "'失败，岗位名称已存在")
        } else if (!postService.checkPostCodeUnique(post)) {
            return error("新增岗位'" + post.postName + "'失败，岗位编码已存在")
        }
        post.createBy=username
        return toAjax(postService.insertPost(post))
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    open fun edit(@Validated @RequestBody post: SysPost): AjaxResult {
        if (!postService.checkPostNameUnique(post)) {
            return error("修改岗位'" + post.postName + "'失败，岗位名称已存在")
        } else if (!postService.checkPostCodeUnique(post)) {
            return error("修改岗位'" + post.postName + "'失败，岗位编码已存在")
        }
        post.updateBy=username
        return toAjax(postService.updatePost(post))
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    open  fun remove(@PathVariable postIds: Array<Long>): AjaxResult {
        return toAjax(postService.deletePostByIds(postIds))
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    open fun optionselect(): AjaxResult {
        val posts= postService.selectPostAll()
        return success(posts)
    }
}
